package com.dc.common.utils;

import com.dc.common.constant.CustomConstants;
import com.dc.common.core.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExpiredCredentialsException;

/**
 * @author zhuangcy
 * @date 2021/6/2
 * @description shiro工具类
 */
public class ShiroUtil {


    /**
     * 获取用户信息
     *
     * @return
     */
    public static LoginUser getUserInfo() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (null == loginUser) {
            throw new ExpiredCredentialsException();
        }
        return loginUser;
    }

    /**
     * 校验token有效性
     *
     * @param token
     * @return
     */
    public static boolean validateSession(String token) {
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        return redisUtil.exists(CustomConstants.SHIRO_SESSION_NAME + token);
    }

}
